// pages/classify/classify.js
var utils = require('../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        currentTab: 0,
        numsye: wx.getStorageSync("number")
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        const res = wx.getSystemInfoSync()
        that.setData({
            height: res.windowHeight - 75
        })
        that.categorylist();
        that.lll();
    },
    onShow: function () {
        this.setData({
            numsye: wx.getStorageSync("number"),
        });
        this.lll();
    },
    lll: function (e) {
        var that = this;
        if (that.data.categoryData != undefined) {
            var categoryData = that.data.categoryData;
            for (var j in categoryData) {
                for (var i in categoryData[j].goods_list) {
                    categoryData[j].goods_list[i].number = 0;
                    for (var k in wx.getStorageSync("listPost")) {
                        if (wx.getStorageSync("listPost")[k].id == categoryData[j].goods_list[i].id) {
                            categoryData[j].goods_list[i].number = wx.getStorageSync("listPost")[k].number;
                        }
                    }
                }
            }
            that.setData({
                categoryData: categoryData
            })
        }
    },
    //分类商品切换接口
    categorylist: function (e) {
        var that = this;
        utils.http("/api/v1/category/category_list", {}, function (res) {
            that.setData({
                navData: res.datas
            })
            that.categoryGoodsList(res.datas[0].id);
        })
    },
    //分类获取商品列表接口
    categoryGoodsList: function (categoryId) {
        var that = this;
        utils.showLoading();
        utils.http("/api/v1/goods/category_goods_list", {
            category_id: categoryId
        }, function (res) {
            utils.hideLoading();
            for (var j in res.datas) {
                  console.log(res.datas[j].goods_list)
                for (var i in res.datas[j].goods_list) {
                  
                    res.datas[j].goods_list[i].number = 0;
                }
            }
            that.setData({
                categoryData: res.datas,
            })
            console.log(that.data.categoryData)
            that.lll();
        })
    },
    //商品详情
    noFrutisDetailsTap: function (e) {
        var id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../commoditydetails/commoditydetails?id=' + id,
        })
    },
    //tab 切换
    swichNav: function (e) {
        var cur = e.currentTarget.dataset.index;
        var id = e.currentTarget.dataset.id;
        if (this.data.currentTaB == cur) {
            return false;
        } else {
            this.setData({
                currentTab: cur
            })
        }
        this.categoryGoodsList(id);
    },
    //减数量
    onNextTap: function (e) {
        var that = this;
        var itemslist = that.data.categoryData;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        if (itemslist[ind]['goods_list'][index].number > 0) {
            itemslist[ind]['goods_list'][index].number = itemslist[ind]['goods_list'][index].number - 1;
            var nowStorage = wx.getStorageSync("listPost");
            for (var i in nowStorage) {
                if (nowStorage[i].id == itemslist[ind]['goods_list'][index].id) {
                    nowStorage[i].number = nowStorage[i].number - 1;
                    if (nowStorage[i].number == 0) {
                        nowStorage.splice(i, 1);
                    }
                }
            }
            that.setData({
                number: wx.getStorageSync("number") - 1,
                categoryData: itemslist
            })
            wx.setStorageSync("listPost", nowStorage)
            wx.setStorageSync("number", that.data.number);
            console.log(wx.getStorageSync("number"))
            // //微信小程序 tabbar 上面设置消息数目等
            if (wx.getStorageSync("number") != 0) {
                wx.setTabBarBadge({
                    index: 3,
                    text: String(wx.getStorageSync("number"))
                })
            } else {
                wx.removeTabBarBadge({
                    index: 3
                })
            }
        } else {
            utils.message("该宝贝宝贝不能减少哦~")
        }

    },
    //加数量
    onPrevTap: function (e) {
        var that = this;
        var itemslist = that.data.categoryData;
        var index = e.currentTarget.dataset.index;
        var ind = e.currentTarget.dataset.ind;
        var num = e.currentTarget.dataset.number;
        var shuzi = wx.getStorageSync("number") || 0;
        var number = shuzi + 1
        that.setData({
            number: number
        })
        wx.setStorageSync("number", number);
        //微信小程序 tabbar 上面设置消息数目等
        if (wx.getStorageSync("number") != 0) {
            wx.setTabBarBadge({
                index: 3,
                text: String(wx.getStorageSync("number"))
            })
        }
        var dataInfo = {
            id: e.currentTarget.dataset.id,
            image: e.currentTarget.dataset.image,
            name: e.currentTarget.dataset.name,
            price: e.currentTarget.dataset.price,
            subtitle: e.currentTarget.dataset.subtitle,
            vip_price: e.currentTarget.dataset.vipprice,
            selectStatus: false, //购物车做删除时候用
            number: 1 //加减数量
        }
        itemslist[ind]['goods_list'][index].number = itemslist[ind]['goods_list'][index].number + 1;
        utils.uniqueSearchRecord(dataInfo);
        that.setData({
            categoryData: itemslist
        })
    },
    //搜索
    onSearchTap: function (e) {
        wx.navigateTo({
            url: '../search/search',
        })
    },
})